<template>
  <div class="cart">
    <h2>购物车</h2>
    <p v-if="!cartItems.length" class="empty-cart">购物车是空的。</p>
    <div v-else>
      <ul class="cart-list">
        <li v-for="item in cartItems" :key="item.id" class="cart-item">
          <img :src="item.image" alt="Product Image" class="item-image" />
          <div class="item-info">
            <h3>{{ item.name }}</h3>
            <p>单价: {{ item.price }} 元</p>
            <div class="item-actions">
              <el-button @click="decreaseQuantity(item)" size="small">-</el-button>
              <span class="quantity">{{ item.quantity }}</span>
              <el-button @click="increaseQuantity(item)" size="small">+</el-button>
              <el-button @click="removeItem(item)" type="danger" size="small">移除</el-button>
            </div>
          </div>
        </li>
      </ul>
      <div class="cart-summary">
        <p>总价值: <strong class="total-price">{{ totalPrice }} 元</strong></p>
        <div class="cart-buttons">
          <el-button type="primary" @click="goHome">返回首页</el-button>
          <el-button type="primary" @click="goProductList">返回产品列表</el-button>
          <el-button type="success" @click="goToPayment">去付款</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useCartStore } from '../stores/cartStore';
import iPhoneImage from '../assets/iPhone16.jpg';
import oppoImage from '../assets/oppofindx8.png';
import samsungImage from '../assets/Samsung_GALAXYS_24.jpg';
import xiaomiImage from '../assets/XIAOMI 15PRO.png';
import nubiaImage from '../assets/nubia Z70 Ultra.jpg';
import vivoImage from '../assets/vivoX200.jpg';
import { computed } from 'vue';

export default {
  setup() {
    const cartStore = useCartStore();

    const cartItems = computed(() =>
        cartStore.items.map((item) => ({
          ...item,
          image:
              item.id === 1
                  ? iPhoneImage
                  : item.id === 2
                      ? oppoImage
                      : item.id === 3
                          ? samsungImage
                          : item.id === 4
                              ? vivoImage
                              : item.id === 5
                                  ? nubiaImage
                                  : item.id === 6
                                      ? xiaomiImage
                                      : null, // 为防止无匹配时的情况
        }))
    );


    const totalPrice = computed(() =>
        cartItems.value.reduce((sum, item) => sum + item.price * item.quantity, 0)
    );

    const increaseQuantity = (item) => {
      cartStore.addItem({...item, quantity: 1});
    };

    const decreaseQuantity = (item) => {
      if (item.quantity > 1) {
        cartStore.addItem({...item, quantity: -1});
      }
    };

    const removeItem = (item) => {
      cartStore.removeItem(item.id);
    };

    const goHome = () => {
      window.location.href = '/';
    };

    const goProductList = () => {
      window.location.href = '/products';
    };

    const goToPayment = () => {
      alert('跳转到付款页面功能尚未实现');
    };

    return {
      cartItems,
      totalPrice,
      increaseQuantity,
      decreaseQuantity,
      removeItem,
      goHome,
      goProductList,
      goToPayment,
    };
  },
};
</script>

<style>
.cart {
  padding: 20px;
}

.cart h2 {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
  color: black;
}

.empty-cart {
  color: black;
  font-size: 16px;
}

.cart ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.cart-item {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background-color: white;
}

.item-image {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 8px;
  margin-right: 15px;
}

.item-info {
  flex: 1;
  color: black;
}

.item-info h3 {
  font-size: 18px;
  margin: 0 0 5px;
}

.item-info p {
  margin: 0 0 10px;
}

.item-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.quantity {
  display: inline-block;
  min-width: 20px;
  text-align: center;
}

.cart-summary {
  margin-top: 20px;
  text-align: right;
  font-size: 18px;
}

.total-price {
  color: black;
}

.cart-buttons {
  margin-top: 20px;
  display: flex;
  gap: 10px;
}

.cart-buttons .el-button {
  flex: 1;
}
</style>
